﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Xml;

namespace NPMonitor.Utility
{
    [Serializable]
    [XmlRoot("config")]
    public class Setting
    {
        [XmlElement("BrowserType")]
        public string BrowserType { get; set; }

        [XmlElement("TimeoutSecs")]
        public int TimeoutSecs { get; set; }

        [XmlElement("JobScreenshotFileFolder")]
        public string JobScreenshotFileFolder { get; set; }

        [XmlElement("JobScreenshotTimeoutFileFolder")]
        public string JobScreenshotTimeoutFileFolder { get; set; }

        [XmlElement("JobScreenshotOtherFileFolder")]
        public string JobScreenshotOtherFileFolder { get; set; }

        //[XmlElement("FiddlerEnable")]
        public bool FiddlerEnable { get; set; }

        //[XmlElement("StartupFlag")]
        public string StartupFlag { get; set; }

        //[XmlElement("listenPort")]
        public int ListenPort { get; set; }

        //[XmlArray("filters")]
        //[XmlArrayItem("url", typeof(string))]
        public string[] Filters { get; set; }

        public string MachineName
        {
            get
            {
                return NPMonitor.Helpers.Utils.GetMachineInfo().MachineName;
            }
        }

        public int PoolId
        {
            get
            {
                string commandText = "select MachinePool.PoolId from MachinePool left join Machines on MachinePool.PoolId=Machines.PoolId where Machines.MachineName='" + MachineName + "'";
                string id = SqlHelper.ExecuteScalar(commandText);

                int poolId = 0;
                int.TryParse(id, out poolId);
                return poolId;
            }
        }

        public bool Matched(string url)
        {
            bool result = false;
            foreach (string filter in Filters)
            {
                if (url.Contains(filter))
                {
                    result = true;
                    break;
                }
            }
            return result;
        }
    }
    
}
